package monitorcon

import (
	"encoding/json"
	"log"
)

//GetStackinfofromredis 从redis返回设备stack info
func GetStackinfofromredis(rs *RedisManager, hostuuid string) string {
	if rs.Deviceinlists(DEVICELISTKEY, hostuuid) == false { //请求的设备不在设备列表中，返回false
		return "error"
	}
	s, error := rs.GetDevicefield(hostuuid, "stack")
	if error != nil {
		return "error"
	}

	return s
}

//GetPortfromredis 从redis返回设备port info
func GetPortfromredis(rs *RedisManager, hostuuid string, position string) string {
	if rs.Deviceinlists(DEVICELISTKEY, hostuuid) == false {
		return "error"
	}

	var s string
	var err error
	if position == "" {
		s, err = rs.GetDevicefield(hostuuid, "port")
		if err != nil {
			return "error"
		}
	}

	log.Println("hostuuid", hostuuid)
	log.Println("p", position)
	if position != "" {
		field := hostuuid + "||" + "port" + "||" + position //||分隔符
		log.Println("field", field)
		s, err = rs.GetDevicefield(hostuuid, field)
		if err != nil {
			return "error"
		}
	}

	return s
}

//GetInterfaceInfofromredis 从redis返回设备interface info
func GetInterfaceInfofromredis(rs *RedisManager, hostuuid string, ifname string) string {
	if rs.Deviceinlists(DEVICELISTKEY, hostuuid) == false {
		return "error"
	}

	var s string
	var err error
	if ifname == "" {

		s, err = rs.GetDevicefield(hostuuid, "interface")
		if err != nil {
			return "error"
		}
	}

	if ifname != "" {
		field := hostuuid + "||" + "interface" + "||" + ifname //||分隔符
		s, err = rs.GetDevicefield(hostuuid, field)
		if err != nil {
			return "error"
		}
	}

	return s
}

//GetCPUInfofromredis 从redis返回设备CPU info
func GetCPUInfofromredis(rs *RedisManager, hostuuid string, position string) string {
	if rs.Deviceinlists(DEVICELISTKEY, hostuuid) == false {
		return "error"
	}

	var s string
	var err error
	if position == "" {

		s, err = rs.GetDevicefield(hostuuid, "cpu")
		if err != nil {
			return "error"
		}
	}

	if position != "" {
		field := hostuuid + "||" + "cpu" + "||" + position //||分隔符
		s, err = rs.GetDevicefield(hostuuid, field)
		if err != nil {
			return "error"
		}
	}

	return s

}

//GetMemoryInfofromredis 从redis返回设备memory info
func GetMemoryInfofromredis(rs *RedisManager, hostuuid string, position string) string {
	if rs.Deviceinlists(DEVICELISTKEY, hostuuid) == false {
		return "error"
	}

	var s string
	var err error
	if position == "" {
		s, err = rs.GetDevicefield(hostuuid, "memory")
		if err != nil {
			return "error"
		}
	}

	if position != "" {
		field := hostuuid + "||" + "memory" + "||" + position //||分隔符
		s, err = rs.GetDevicefield(hostuuid, field)
		if err != nil {
			return "error"
		}
	}

	return s

}

//GetMacInfofromredis 从redis返回设备mac info
func GetMacInfofromredis(rs *RedisManager, hostuuid string, id string) string {
	if rs.Deviceinlists(DEVICELISTKEY, hostuuid) == false {
		return "error"
	}

	var s string
	var err error
	if id == "" {

		s, err = rs.GetDevicefield(hostuuid, "mac")
		if err != nil {
			return "error"
		}
	}

	if id != "" {
		field := hostuuid + "||" + "mac" + "||" + id //||分隔符
		s, err = rs.GetDevicefield(hostuuid, field)
		if err != nil {
			return "error"
		}
	}

	return s

}

//Subscribefieldinfofromredis 保存需要订阅的field信息到订阅列表
func Subscribefieldinfofromredis(rs *RedisManager, hostuuid string, field string, item string) bool {
	var fieldvalue string
	if item == "" {
		fieldvalue = hostuuid + "||" + field
		if rs.Deviceinlists(SUBSCRIBFIELDLIST, fieldvalue) == true { //请求订阅的设备在订阅列表中，无需重复订阅
			return false
		}
	} else {
		fieldvalue = hostuuid + "||" + field + "||" + item
		if rs.Deviceinlists(SUBSCRIBFIELDITEMLIST, fieldvalue) == true { //请求订阅的设备在订阅列表中，无需重复订阅
			return false
		}
	}

	if item == "" {
		fieldvalue = hostuuid + "||" + field
		rs.SubscribeDevicefield(fieldvalue)

	} else {
		fieldvalue = hostuuid + "||" + field + "||" + item
		rs.SubscribeDevicefielditem(fieldvalue)
	}

	return true
}

//GetSwitchInfofromredis 得到北向设备信息集成 (1.1)
func GetSwitchInfofromredis(rs *RedisManager, switchuuid string) string {
	map1 := make(map[string]interface{})
	map1["@odata.id"] = "" //deafult

	map1["StackStatus"] = "Stack" //default
	map1["SoftwareVersion"] = "6.02"
	map1["IpAddr"] = "168.163.1.100"
	map1["NetMask"] = "255.255.255.0"
	map1["CpuInfo"] = GetFieldinfo(rs, switchuuid, "cpu","1.1")
	map1["MemoryInfo"] = GetFieldinfo(rs, switchuuid, "memory","1.1")
	map1["PortInfo"] = GetFieldinfo(rs, switchuuid, "port","1.1")
	map1["MacTables"] = GetFieldinfo(rs, switchuuid, "mac","1.1")
	b, err := json.Marshal(map1)
	if err != nil {
		return ""
	}
	log.Println("json str:", b)
	return string(b)
}

//GetMacmapInfofromredis 得到北向需要的mac信息
func GetMacmapInfofromredis(rs *RedisManager, switchuuid string) List {

	list := GetFieldinfo(rs, switchuuid, "mac","1.2")

	return list
}

//GetPortmapInfofromredis 得到北向需要的port信息
func GetPortmapInfofromredis(rs *RedisManager, switchuuid string) List {

	list := GetFieldinfo(rs, switchuuid, "port","1.2")

	return list
}
